

//
//  WelcomeViewController.m
//  kuangjia
//
//  Created by 张帅 on 15/10/1.
//  Copyright © 2015年 ZhangShuai. All rights reserved.
//

#import "WelcomeViewController.h"

#import <UIImageView+WebCache.h>

#import <UIImageView+HighlightedWebCache.h>
#import "UserAccountViewModel.h"


@interface WelcomeViewController ()


@property(nonatomic,strong) UIImageView *backImageView;

@property(nonatomic,strong) UIImageView *iconView;

@property(nonatomic,strong) UILabel *label;

@property(nonatomic,strong) NSLayoutConstraint *iconBottomCons;

@end
@implementation WelcomeViewController


-(void)viewDidLoad{
    [super viewDidLoad];
  
    [self setupUI];

    NSURL *url =[UserAccountViewModel sharedUserAccount].avatarUrl;

    [self.iconView sd_setImageWithURL:url];
}




- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    CGFloat h  =  -([UIScreen mainScreen].bounds.size.height + self.iconBottomCons.constant );
    
    self.iconBottomCons.constant = h;
    
    self.label.alpha = 0;
    
    [UIView animateWithDuration:1.2 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:10 options:UIViewAnimationOptionAllowUserInteraction animations:^{
   
        [self.view layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1 animations:^{
            
            self.label.alpha = 1;
            
            
            
            [[NSNotificationCenter defaultCenter] postNotificationName:HMSwitchRootViewControllerNotification object:nil];
        }];
    }];
    
}
-(void)setupUI{

    
    [self.view addSubview:self.backImageView];
    [self.view addSubview:self.iconView];
    [self.view addSubview:self.label];
    self.iconView.translatesAutoresizingMaskIntoConstraints =NO;
    self.backImageView.translatesAutoresizingMaskIntoConstraints =NO;
    self.label.translatesAutoresizingMaskIntoConstraints =NO;
    
    //MARK: 背景的自动布局
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[v]-0-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:@{@"v": self.backImageView}]];
    
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[v]-0-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:@{@"v": self.backImageView}]];
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:(NSLayoutAttributeWidth) relatedBy:(NSLayoutRelationEqual) toItem:nil attribute:(NSLayoutAttributeWidth) multiplier:1 constant:90]];
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:(NSLayoutAttributeHeight) relatedBy:(NSLayoutRelationEqual) toItem:nil attribute:(NSLayoutAttributeHeight) multiplier:1 constant:90]];
    
 
    
    //MARK: 头像
 
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:(NSLayoutAttributeCenterX) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:(NSLayoutAttributeCenterX) multiplier:1 constant:0] ];
    

    self.iconBottomCons =[NSLayoutConstraint constraintWithItem:self.iconView attribute:(NSLayoutAttributeCenterY) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:(NSLayoutAttributeBottom) multiplier:1 constant:-250];
    
    
    [self.view addConstraint:self.iconBottomCons];
    
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label attribute:(NSLayoutAttributeCenterX) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:(NSLayoutAttributeCenterX) multiplier:1 constant:0]];
    
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:self.iconView attribute:(NSLayoutAttributeBottom) multiplier:1 constant:16]];
    
    
    
}



-(UILabel *)label
{  if(_label ==nil)
    
    _label=[[UILabel alloc] init];
    
    [_label setText:@"欢迎归来"];
    
    return _label;
}


-(UIImageView *)iconView{

    if (_iconView ==nil) {
        _iconView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
     
        _iconView.layer.cornerRadius = 45;
        _iconView.layer.masksToBounds = YES;
        
        [_iconView sizeToFit];
    }
    return _iconView;
}


-(UIImageView *)backImageView{

    if (_backImageView==nil) {
        _backImageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_background"]];
        }
    
    return _backImageView;
}





@end
